package shiro.redis;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;

import com.jfinal.plugin.redis.Redis;

/**
 * 使用 JFinal RedisPlugin 提供缓存服务
 */
public class RedisManager {
     
	private static final Logger log = Logger.getLogger(RedisManager.class);
 
    /**
     * 初始化方法
     */
    public static void init()
    {
    	log.info("+++++++++++++++++++++++++++++");
    	log.info("Shiro Redis 使用 AppConfig 初始化的 Redis 缓存!");
    	log.info("+++++++++++++++++++++++++++++");
    }
    
    public static Jedis getJedis() 
	{
		return Redis.use().getJedis();
	}
    
    public static int getExpireSeconds()
    {
        return 60 * 60 * 3;
    }
    
    public static <K, V> V get(K key) 
    {
        return Redis.use().get(key);
    }
    
 
    public static <K, V> V put(K key, V value) 
    {
        Redis.use().set(key, value);
        return value;
    }
    
    public static <K, V> void put(K key, V value, int seconds) 
	{
		Redis.use().setex(key, seconds, value);
	}
 
    public static <K, V> V remove(K key) //throws CacheException 
    {
    	V value = get(key);
    	Redis.use().del(key);
    	return value;
    }
    
    public static <K> void del(K key) 
    {
    	Redis.use().del(key);
    }
    
    
    
    
    
	/*public byte[] get(byte[] key) 
    {
		byte[] value = null;
        Jedis jedis = getJedis();
        try {
            value = jedis.get(key);
        } finally 
        {
        	colse(jedis);
        }
        
        log.info("shiro redis：从缓存读取 byte[]  key = " + new String(key));
        
        return value;
    }
   
	public String get(String key) 
	{
		String value=null;
        Jedis jedis = getJedis();
        try {
            value = jedis.get(key);
        } finally {
            colse(jedis);
        }
        
        log.info("shiro redis：从缓存读取 String  value = " + value);
        
        return value;
    }
 
   
	public byte[] set(byte[] key, byte[] value) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
            if (getExpire() != 0) 
            {
                jedis.expire(key, getExpire());
            }
        } finally {
            colse(jedis);
        }
        return value;
    }
   
	
	public String set(String key, String value) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
            if (getExpire() != 0) 
            {
                jedis.expire(key, getExpire());
            }
        } finally {
            colse(jedis);
        }
        return value;
    }
 
	public byte[] set(byte[] key, byte[] value, int expire) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            colse(jedis);
        }
        return value;
    }
   
	
	public String set(String key,String value, int expire) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            colse(jedis);
        }
        return value;
    }
 
    
	public void del(byte[] key) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.del(key);
        } finally {
            colse(jedis);
        }
    }
    
	
	public void del(String key) 
	{
        Jedis jedis = getJedis();
        try {
            jedis.del(key);
        } finally {
            colse(jedis);
        }
    }*/
 
	public static void flushDB() 
	{
        Jedis jedis = getJedis();
        try {
            jedis.flushDB();
        } finally {
            colse(jedis);
        }
    }
 
	public static Long dbSize() 
	{
        Long dbSize = 0L;
        Jedis jedis = getJedis();
        try {
            dbSize = jedis.dbSize();
        } finally {
            colse(jedis);
        }
        return dbSize;
    }
 
	/*public static Set<byte[]> keys(String pattern) 
	{
        Set<byte[]> keys = null;
        Jedis jedis = getJedis();
        try {
            keys = jedis.keys(pattern.getBytes());
        } finally {
            colse(jedis);
        }
        return keys;
    }*/
 
	private static void colse(Jedis jedis) 
	{
		if (jedis != null) 
		{
			jedis.close();
		}
	}
	
}